package com.darren.euc.action.member;

import com.darren.euc.entity.ClientParamsEntity;
import com.darren.euc.entity.Member;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.service.ClientParamsEntityService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Machenike-Pc on 2018/4/5.
 */
@Controller
@RequestMapping("/clientparams")
public class ClientParamsAction {
    @Autowired
    private ClientParamsEntityService clientParamsEntityService;
    /**
     * 设置上班（下班）时间
     */
    @ResponseBody
    @RequestMapping(value = "/setJobTime",method = RequestMethod.POST)
    public ResponseJSON setStartAndEndTime(@RequestBody JSONObject jsonObject,HttpServletResponse response, HttpServletRequest request){
        try{
            Member member = (Member) request.getAttribute("member");
            if (member == null || member.getIsAdmin() == 0) {
                return new ResponseJSON(-1, "没有权限", null);
            }
            List<ClientParamsEntity> clientParamsEntity = clientParamsEntityService.setJobStartAndEndTime(jsonObject.getString("startTime"),jsonObject.getString("endTime"),member);
            return ResponseJSON.defaultSuccessResponse(clientParamsEntity);
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseJSON().set(-1,e.getMessage(),null);
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getJobTime",method = RequestMethod.GET)
    public ResponseJSON setStartAndEndTime( HttpServletResponse response, HttpServletRequest request){
        try{
            Member member = (Member) request.getAttribute("member");
            if (member == null || member.getIsAdmin() == 0) {
                return new ResponseJSON(-1, "没有权限", null);
            }
            List<ClientParamsEntity> clientParamsEntity = clientParamsEntityService.findJobTimeByClientId(member.getClientId());
            if(clientParamsEntity == null){
                return new ResponseJSON().set(-1,"该企业还没有设置上下班时间",null);
            }
            return ResponseJSON.defaultSuccessResponse(clientParamsEntity);
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseJSON().set(-1,e.getMessage(),null);
        }
    }
}
